import re
import csv

def get_dict_from_sequence(seq, key):
    return dict((d[key], dict(d, index=index)) for (index, d) in enumerate(seq))

def main():
	for year in range (2008,2013):
		
		# get bracket data
		data = csv.reader(open("results"+str(year)+".csv"))
		fields = data.next()
		results = []
		for row in data:
			items = zip(fields,row)
			item = {}
			for (name, value) in items:
				item[name] = re.sub(r'[^\w-]','',re.sub(r' ','-',value.strip().lower()))
			results.append(item)
			
		# get stat data
		data = csv.reader(open("teamstats_"+str(year)+".csv"))
		fields = data.next()
		stats = []
		for row in data:
			items = zip(fields,row)
			item = {}
			for (name, value) in items:
				item[name] = value.strip()
			stats.append(item)

		print("TeamA\tAWinPct\tTeamB\tBWinPct\tScore")
		ind = get_dict_from_sequence(stats, key="Team")
		for r in results:
			indA = ind[r["Winner"]]["index"]
			indB = ind[r["Loser"]]["index"]
			print(r["Winner"]+"\t"+stats[indA]["WinningPct"]+"\t"+r["Loser"]+"\t"+stats[indB]["WinningPct"]+"\t"+r["Score"])

if __name__ == "__main__":
    main()
	

	
